// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import login from '../pages/Login.vue'
import add from '../pages/Add.vue'
import list from '../pages/List.vue'
import report from '../pages/Report.vue'
import report02 from '../pages/Report02.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import layout01 from '../layout/Layout01.vue'
import layout02 from '../layout/Layout02.vue'
import layoutAi from '../layout/Layout-ai.vue'

import Doc from '../pages-ai/Doctor.vue';  
import Hp from '../pages-ai/Helper.vue';
import imgCreater from '../pages-ai/imgCreater.vue';
import Game from '../pages-yl/Game.vue';

const routes = [
  {
    path: '/ai',
    component: layoutAi,
    children:[
      {
        path: 'helper',
        name: 'helper',
        component: Hp
      },
      {
        path: 'doctor',
        name: 'doctor',
        component: Doc
      },
      {
        path: 'img',
        name: 'img',
        component: imgCreater
      }
    ]
  },
  {
    path: '/b',
    component: layout02,
    children:[
      {
        path: '/',
        component: login
      },
      {
        path: '/login',
        name: 'login',
        component: login
      },
      {
        path: '/game',
        name: 'game',
        component: Game
      }
    ]
  },
  {
    path: '/a',
    component: layout01,
    children:[
      {
        path: '/add',
        name: 'add',
        component: add,
        meta:{
          needLogin:true
        }
      },
      {
        path: '/list',
        name: 'list',
        component: list,
        meta:{
          needLogin:true
        }
      },
      {
        path: '/report',
        name: 'report',
        component: report,
        meta:{
          needLogin:true,
          roles:["VIP","SVIP"]
        }
      },
      {
        path: '/report02',
        name: 'report02',
        component: report02,
        meta:{
          needLogin:true,
          roles:["SVIP"]
        }
      }
    ]
  },
  

  
 
  
];

const router = createRouter({
  history: createWebHistory(),  // HTML5历史模式
  routes
});

//添加拦截器，控制权限
router.beforeEach((to, from, next) => {
  //alert("你想跳转从"+from.path+"--》"+to.path);
  let ss=sessionStorage.getItem("curUser");
  if(to.meta.needLogin&&ss==null){
    ElMessageBox.alert("请先登录",'提示')
    next("/login");
    return;
  }
  if(to.meta.roles){
     let user=JSON.parse(ss);
     if(to.meta.roles.includes(user.role)==false){
         ElMessageBox.alert("您无权限访问此页面，请升级为"+to.meta.roles,"警告")
         return;
         //next("/list")
     }
  }
  next();//放行
})

export default router;